package com.ruoyi.basebill.service.impl;

import com.ruoyi.basebill.DTO.*;
import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.service.*;
import com.ruoyi.basedata.domain.WfzBaseunit;
import com.ruoyi.basedata.domain.WfzPerson;
import com.ruoyi.basedata.service.IWfzBaseunitService;
import com.ruoyi.basedata.service.IWfzPersonService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.basedata.domain.WfzPerson;
import com.ruoyi.basedata.service.IWfzPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @projectName: wfz
 * @package: com.ruoyi.basebill.service.impl
 * @className: IApiServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2023/1/6 16:29
 * @version: 1.0
 */
@Service
public class ApiServiceImpl implements IApiService {

    @Autowired
    private ITWfzRemunerationService tWfzRemunerationService;

    @Autowired
    private IWfzXczbsbService wfzXczbsbService;

    @Autowired
    private IWfzPersonService wfzPersonService;

    @Autowired
    private IWfzBaseunitService wfzBaseunitService;

    @Autowired
    private IWfzJtgzflService wfzJtgzflService;


    /**
     * 薪酬总表
     * @param remunerationInfo
     * @param tWfzRemuneration
     * @return
     */
    @Override
    public int importRemuneration(RemunerationInfo remunerationInfo, TWfzRemuneration tWfzRemuneration,AjaxResult result) {

        String fnumber = remunerationInfo.getFnumber();
        String fcompany = remunerationInfo.getFcompany();
        String fperiod = remunerationInfo.getFperiod();
        String fsourcebillid = remunerationInfo.getFsourcebillid();


        tWfzRemuneration.setFnumber(fnumber);
        tWfzRemuneration.setFcompany(fcompany);
        tWfzRemuneration.setFperiod(fperiod);
        tWfzRemuneration.setFsourcebillid(fsourcebillid);
        List<RemunerationEntryInfo> entry = remunerationInfo.getEntry();
        ArrayList<TWfzRemunerationentry> tWfzRemunerationentries = new ArrayList<>();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("Over");
        for(int i=0;i<entry.size();i++){
            TWfzRemunerationentry tWfzRemunerationentry = new TWfzRemunerationentry();
            RemunerationEntryInfo remunerationEntryInfo = entry.get(i);
//            String fcostName = remunerationEntryInfo.getFcostName();
//
//            String fcostNumber = remunerationEntryInfo.getFcostNumber();
//            String fcostcompany = remunerationEntryInfo.getFcostcompany();
            String fpersonNumber = remunerationEntryInfo.getFpersonNumber();
            //成本中心根据人进行映射，从员工档案上取
            WfzPerson person = new WfzPerson();
            person.setFnumber(fpersonNumber);
            List<WfzPerson> wfzPeoplelist = wfzPersonService.selectWfzPersonList(person);
            if(wfzPeoplelist.size()>0){
                WfzPerson person1 = wfzPeoplelist.get(0);
                String fsscost = person1.getFsscost();
                String fsscostname = person1.getFsscostname();//成本中心名称
                String fcostcompany1 = person1.getFcostcompany(); //成本中心所属的财务组织
                String fsscompany = person1.getFsscompany(); //一级法人公司对应成本中心财务组织
                String fsbffgs = person1.getFsbffgs();//社保缴纳单位
                String fgjjffgs = person1.getFgjjffgs();//公积金缴纳单位
                tWfzRemunerationentry.setFcostcompany(fcostcompany1);
                tWfzRemunerationentry.setFcostname(fsscostname);
                tWfzRemunerationentry.setFcostnumber(fsscost);
                tWfzRemunerationentry.setFsbdw(fsbffgs);
                tWfzRemunerationentry.setFgjjdw(fgjjffgs);
                tWfzRemunerationentry.setFcompany(fsscompany);
            }else{
                stringBuffer.append("员工"+fpersonNumber+"未同步到数据中台");
                continue;
            }
            BigDecimal fyfgz = remunerationEntryInfo.getFyfgz();
            BigDecimal fylbx = remunerationEntryInfo.getFylbx();
            BigDecimal fyybx = remunerationEntryInfo.getFyybx();
            BigDecimal fsybx = remunerationEntryInfo.getFsybx();
            BigDecimal fgsbx = remunerationEntryInfo.getFgsbx();
            BigDecimal fgryl = remunerationEntryInfo.getFgryl();
            BigDecimal fgryy = remunerationEntryInfo.getFgryy();
            BigDecimal fgrsy = remunerationEntryInfo.getFgrsy();
            BigDecimal fbkgryl = remunerationEntryInfo.getFbkgryl();
            BigDecimal fbkgryy = remunerationEntryInfo.getFbkgryy();
            BigDecimal fbkgrsy = remunerationEntryInfo.getFbkgrsy();
            BigDecimal fgjj = remunerationEntryInfo.getFgjj();
            BigDecimal fgrgjj = remunerationEntryInfo.getFgrgjj();
            BigDecimal fgfjf = remunerationEntryInfo.getFgfjf();
            BigDecimal fjrfl = remunerationEntryInfo.getFjrfl();
            BigDecimal fgwbnf = remunerationEntryInfo.getFgwbnf();
            BigDecimal ffhbfl = remunerationEntryInfo.getFfhbfl();
            BigDecimal fsssdf = remunerationEntryInfo.getFsssdf();
            BigDecimal fscnzj = remunerationEntryInfo.getFscnzj();
            BigDecimal fxznzj = remunerationEntryInfo.getFxznzj();
            BigDecimal fnzjgs = remunerationEntryInfo.getFnzjgs();
            BigDecimal ffk = remunerationEntryInfo.getFfk();
            BigDecimal fsfhj = remunerationEntryInfo.getFsfhj();
            int fislz = remunerationEntryInfo.getFislz();
            int fissbdy = remunerationEntryInfo.getFissbdy();
            String fbankName = remunerationEntryInfo.getFbankName();

//            String fsbdw = remunerationEntryInfo.getFsbdw();
//            String fgjjdw = remunerationEntryInfo.getFgjjdw();

            //从人上面取


            //保险类单据
            BigDecimal fbkgsbx = remunerationEntryInfo.getFbkgsbx();
            BigDecimal fbksybx = remunerationEntryInfo.getFbksybx();
            BigDecimal fbksyubx = remunerationEntryInfo.getFbksyubx();
            BigDecimal fbkyybx = remunerationEntryInfo.getFbkyybx();
            BigDecimal fbkylbx = remunerationEntryInfo.getFbkylbx();
            BigDecimal fbkgrzf = remunerationEntryInfo.getFbkgrzf();
            BigDecimal fgrgfjf = remunerationEntryInfo.getFgrgfjf();

            BigDecimal fdwbkgjj = remunerationEntryInfo.getFdwbkgjj();

//            tWfzRemunerationentry.setFcostcompany(fcostcompany);
//            tWfzRemunerationentry.setFcostname(fcostName);
//            tWfzRemunerationentry.setFcostcompany(fcostNumber);
            tWfzRemunerationentry.setFyfgz(fyfgz);
            tWfzRemunerationentry.setFylbx(fylbx);
            tWfzRemunerationentry.setFyybx(fyybx);
            tWfzRemunerationentry.setFsybx(fsybx);
            tWfzRemunerationentry.setFgsbx(fgsbx);
            tWfzRemunerationentry.setFgryl(fgryl);
            tWfzRemunerationentry.setFgryy(fgryy);
            tWfzRemunerationentry.setFgrsy(fgrsy);
            remunerationEntryInfo.setFbkgsbx(fbkgsbx);
            remunerationEntryInfo.setFbksybx(fbksybx);
            remunerationEntryInfo.setFbksyubx(fbksyubx);
            remunerationEntryInfo.setFbkylbx(fbkylbx);
            remunerationEntryInfo.setFbkyybx(fbkyybx);

            tWfzRemunerationentry.setFbkgryl(fbkgryl);
            tWfzRemunerationentry.setFbkgryy(fbkgryy);
            tWfzRemunerationentry.setFbkgrsy(fbkgrsy);
            tWfzRemunerationentry.setFgjj(fgjj);
            tWfzRemunerationentry.setFgrgjj(fgrgjj);
            tWfzRemunerationentry.setFgfjf(fgfjf);
            tWfzRemunerationentry.setFjrfl(fjrfl);
            tWfzRemunerationentry.setFgwbnf(fgwbnf);
            tWfzRemunerationentry.setFfhbfl(ffhbfl);
            tWfzRemunerationentry.setFsssdf(fsssdf);
            tWfzRemunerationentry.setFscnzj(fscnzj);
            tWfzRemunerationentry.setFxznzj(fxznzj);
            tWfzRemunerationentry.setFnzjgs(fnzjgs);
            tWfzRemunerationentry.setFfk(ffk);
            tWfzRemunerationentry.setFbkgrzf(fbkgrzf);
            tWfzRemunerationentry.setFdwbkgjj(fdwbkgjj);
            tWfzRemunerationentry.setFgrgfjf(fgrgfjf);
            tWfzRemunerationentry.setFsfhj(fsfhj);
            tWfzRemunerationentry.setFislz((long)fislz);
            tWfzRemunerationentry.setFissbdy((long)fissbdy);
            tWfzRemunerationentry.setFbankname(fbankName);

            tWfzRemunerationentry.setFpersonnumber(fpersonNumber);

            tWfzRemunerationentries.add(tWfzRemunerationentry);

        }


        tWfzRemuneration.setTWfzRemunerationentryList(tWfzRemunerationentries);
        if (!"Over".equals(stringBuffer.toString())){
            result.put("msg",stringBuffer.toString());
            result.put("state","0");
            return 0;
        }
        int i = tWfzRemunerationService.insertTWfzRemuneration(tWfzRemuneration);
        return i;
    }


    /**
     * 当月社保数据
     * @param xczbsbInfo
     * @param wfzXczbsb
     * @param result
     * @return
     */
    @Override
    public int importXczbsb(XczbsbInfo xczbsbInfo, WfzXczbsb wfzXczbsb, AjaxResult result) {
        StringBuffer stringBuffer =new StringBuffer();
        stringBuffer.append("Over");
        String fnumber = xczbsbInfo.getFnumber();
        String fperiod = xczbsbInfo.getFperiod();
        String fsourcebillid = xczbsbInfo.getFsourcebillid();
//        String fxzlx = xczbsbInfo.getFxzlx();

        wfzXczbsb.setFnumber(fnumber);
        wfzXczbsb.setFperiod(fperiod);
        wfzXczbsb.setFsourcebillid(fsourcebillid);

        List<XczbsbEntryInfo> entry = xczbsbInfo.getEntry();
        ArrayList<WfzXczbsbentry> wfzXczbsbentries = new ArrayList<>();
        for (int i = 0; i < entry.size(); i++) {
            WfzXczbsbentry wfzXczbsbentry = new WfzXczbsbentry();

            XczbsbEntryInfo xczbsbEntryInfo = entry.get(i);
            String fpersonNumber = xczbsbEntryInfo.getFpersonNumber();//员工编号
            BigDecimal fyfgz = xczbsbEntryInfo.getFyfgz();//应发工资
            BigDecimal fylbx = xczbsbEntryInfo.getFylbx();//养老保险
            BigDecimal fyybx = xczbsbEntryInfo.getFyybx();//医疗保险
            BigDecimal fsybx = xczbsbEntryInfo.getFsybx();//失业保险
            BigDecimal fgsbx = xczbsbEntryInfo.getFgsbx();//工伤保险
            BigDecimal fsyubx = xczbsbEntryInfo.getFsyubx();//生育保险
            BigDecimal fbkylbx = xczbsbEntryInfo.getFbkylbx();//补扣养老保险
            BigDecimal fbkyybx = xczbsbEntryInfo.getFbkyybx();//补扣医疗保险
            BigDecimal fbksybx = xczbsbEntryInfo.getFbksybx();//补扣失业保险
            BigDecimal fbkgsbx = xczbsbEntryInfo.getFbkgsbx();//补扣工伤保险
            BigDecimal fbksyubx = xczbsbEntryInfo.getFbksyubx();//补扣生育保险
            String fbankName = xczbsbEntryInfo.getFbankName();//银行开户行
            Long fislz = xczbsbEntryInfo.getFislz();//是否离职
            String fsbdw = xczbsbEntryInfo.getFsbdw();//社保缴纳单位

            wfzXczbsbentry.setFpersonnumber(fpersonNumber);
            wfzXczbsbentry.setFyfgz(fyfgz);
            wfzXczbsbentry.setFylbx(fylbx);
            wfzXczbsbentry.setFyybx(fyybx);
            wfzXczbsbentry.setFsybx(fsybx);
            wfzXczbsbentry.setFgsbx(fgsbx);
            wfzXczbsbentry.setFsyubx(fsyubx);
            wfzXczbsbentry.setFbkylbx(fbkylbx);
            wfzXczbsbentry.setFbkyybx(fbkyybx);
            wfzXczbsbentry.setFbksybx(fbksybx);
            wfzXczbsbentry.setFbkgsbx(fbkgsbx);
            wfzXczbsbentry.setFbksyubx(fbksyubx);
            wfzXczbsbentry.setFbankname(fbankName);
            wfzXczbsbentry.setFislz(fislz);
            wfzXczbsbentry.setFsbdw(fsbdw);


            //根据员工编码还原员工档案对象
            //获取数据：成本中心编码、成本中心名称、成本中心所属财务组织
            String fsscostname;//成本中心名称
            String fsscost = null;//成本中心
            String fcostcompany;//成本中心所属财务组织
            WfzPerson wfzPerson = new WfzPerson();
            wfzPerson.setFnumber(fpersonNumber);
            List<WfzPerson> wfzPeople = wfzPersonService.selectWfzPersonList(wfzPerson);
            if (wfzPeople.size() >0 ){
                wfzPerson = wfzPeople.get(0);
                fsscostname = wfzPerson.getFsscostname();//成本中心
                fsscost = wfzPerson.getFsscost();//成本中心名称
                fcostcompany = wfzPerson.getFcostcompany();//成本中心所属财务组织
                String fsbffgs = wfzPerson.getFsbffgs();
                wfzXczbsbentry.setFcostnumber(fsscost);
                wfzXczbsbentry.setFcostname(fsscostname);
                wfzXczbsbentry.setFcostcompany(fcostcompany);
                wfzXczbsbentry.setFsbdw(fsbffgs); //社保缴纳单位
            }else {
//                result.put("msg","没有找到员工编码为：" + fpersonNumber + "的对应员工档案对象");
//                result.put("state","0");
//                return -1;
                stringBuffer.append("没有找到员工编码为：" + fpersonNumber + "的对应员工档案对象");
                continue;
            }
            //根据成本中心编码还原对象获取凭证字数据
            String fpzz = null;
            WfzBaseunit wfzBaseunit = new WfzBaseunit();
            wfzBaseunit.setFgynumber(fsscost);
            List<WfzBaseunit> wfzBaseunits = wfzBaseunitService.selectWfzBaseunitList(wfzBaseunit);
            if (wfzBaseunits.size() > 0){
                wfzBaseunit = wfzBaseunits.get(0);
                fpzz = wfzBaseunit.getFpzz();
                wfzXczbsbentry.setFvourtype(fpzz);
            }else {
//                result.put("msg","没有找到成本中心编码为：" + fsscost + "的对应组织成本中心对象");
//                result.put("state","0");
//                return -1;
                stringBuffer.append("没有找到成本中心编码为：" + fsscost + "的对应组织成本中心对象");
                continue;
            }
            //组织暂定
            wfzXczbsbentries.add(wfzXczbsbentry);

        }

        if (!"Over".equals(stringBuffer.toString())){
            result.put("msg",stringBuffer.toString());
            result.put("state","0");
            return 0;
        }

        wfzXczbsb.setWfzXczbsbentryList(wfzXczbsbentries);
        int i = wfzXczbsbService.insertWfzXczbsb(wfzXczbsb);
        return i;
    }

    @Override
    public int importYgda(YgdaInfo ygdaInfo, WfzPerson wfzPerson) {
        String fnumber = ygdaInfo.getFnumber();
        String fname = ygdaInfo.getFname();//
        String fssCompany = ygdaInfo.getFssCompany();//
        String fssCostName = ygdaInfo.getFssCostName();//
        String fssCost = ygdaInfo.getFssCost();//
        String fgzcdgs = ygdaInfo.getFgzcdgs();//
        String fgzffgs = ygdaInfo.getFgzffgs();//
        String fcostcompany = ygdaInfo.getFcostcompany();//
        String fsbcdgs = ygdaInfo.getFsbcdgs();//
        String fsbffgs = ygdaInfo.getFsbffgs();//
        String fgjjcdgs = ygdaInfo.getFgjjcdgs();//
        String fgjjffgs = ygdaInfo.getFgjjffgs();//
        String fgzffjrjg = ygdaInfo.getFgzffjrjg();

        wfzPerson.setFnumber(fnumber);
        wfzPerson.setFname(fname);
        wfzPerson.setFsscompany(fssCompany);
        wfzPerson.setFsscostname(fssCostName);
        wfzPerson.setFsscost(fssCost);
        wfzPerson.setFgzcdgs(fgzcdgs);
        wfzPerson.setFgzffgs(fgzffgs);
        wfzPerson.setFcostcompany(fcostcompany);
        wfzPerson.setFsbcdgs(fsbcdgs);
        wfzPerson.setFsbffgs(fsbffgs);
        wfzPerson.setFgjjcdgs(fgjjcdgs);
        wfzPerson.setFgjjffgs(fgjjffgs);
        wfzPerson.setFgzffjrjg(fgzffjrjg);




        WfzPerson wfzPerson1 = new WfzPerson();
        wfzPerson1.setFnumber(fnumber);
        List<WfzPerson> wfzPersonList = wfzPersonService.selectWfzPersonList(wfzPerson1);
        int i = 0;
        if (wfzPersonList.size()>0){
            i = wfzPersonService.updateWfzPerson(wfzPerson);
        }else{
            i = wfzPersonService.insertWfzPerson(wfzPerson);
        }
        return i;
    }

    @Override
    public int importJtgzfl(JtgzflInfo JtgzflInfo, WfzJtgzfl wfzJtgzfl, AjaxResult result) {

        String fnumber = JtgzflInfo.getFnumber(); //单据编号
        String fcompany = JtgzflInfo.getFcompany(); //组织
        String fperiod = JtgzflInfo.getFperiod();
        String fsourcebillid = JtgzflInfo.getFsourcebillid();


        wfzJtgzfl.setFnumber(fnumber);
        wfzJtgzfl.setFcompany(fcompany);
        wfzJtgzfl.setFperiod(fperiod);
        wfzJtgzfl.setFsourcebillid(fsourcebillid);
        List<JtgzflEntryInfo> entry = JtgzflInfo.getEntry();

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("Over");
        ArrayList<WfzJtgzflentry> wfzJtgzflentries = new ArrayList<>();
        for (JtgzflEntryInfo jtgzflEntryInfo : entry) {
            WfzJtgzflentry wfzJtgzflentry = new WfzJtgzflentry();
            String fpersonNumber = jtgzflEntryInfo.getFpersonNumber();
            BigDecimal fyfgz = jtgzflEntryInfo.getFyfgz();

            //员工还原相应的员工档案
            WfzPerson wfzPerson = new WfzPerson();
            wfzPerson.setFnumber(fpersonNumber);
            List<WfzPerson> wfzPeople = wfzPersonService.selectWfzPersonList(wfzPerson);

            if(wfzPeople.size()<0){
                stringBuffer.append("员工"+fpersonNumber+"未同步到数据中台");
                continue;
            }
            WfzPerson person = wfzPeople.get(0);
            String fsscost = person.getFsscost(); //成本中心编码
            String fsscostname = person.getFsscostname(); //成本中心名称
            String fcostcompany = person.getFcostcompany(); //成本中心所属财务组织
            String fsscompany = person.getFsscompany();
            wfzJtgzflentry.setFcompany(fsscompany);
            wfzJtgzflentry.setFpersonnumber(fpersonNumber);
            wfzJtgzflentry.setFcostname(fsscostname);
            wfzJtgzflentry.setFcostnumber(fsscost);
            wfzJtgzflentry.setFcostcompany(fcostcompany);
            wfzJtgzflentry.setFyfgz(fyfgz);
            wfzJtgzflentry.setFtzhje(fyfgz);
            //成本中心属性
            WfzBaseunit wfzBaseunit = new WfzBaseunit();
            wfzBaseunit.setFnumber(fsscost);
            List<WfzBaseunit> wfzBaseunits = wfzBaseunitService.selectWfzBaseunitList(wfzBaseunit);
            if(wfzBaseunits.size()<0){
                stringBuffer.append("成本中心"+fsscost+"在数据中台成本中心未找到该组织");
                continue;
            }
            WfzBaseunit baseunit = wfzBaseunits.get(0);
            String fcosttype = baseunit.getFcosttype(); //成本中心属性
            wfzJtgzflentry.setFcbzxxz(fcosttype);
            wfzJtgzflentries.add(wfzJtgzflentry);
        }
        wfzJtgzfl.setWfzJtgzflentryList(wfzJtgzflentries);

        if (!"Over".equals(stringBuffer.toString())){
            result.put("msg",stringBuffer.toString());
            result.put("state","0");
            return 0;
        }


        return wfzJtgzflService.insertWfzJtgzfl(wfzJtgzfl);
    }
}
